from flask import Flask, request, render_template, jsonify
import numpy as np
from keras.models import load_model
model = load_model('net.h5')#加载神经网络模型
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
    data = request.get_json()  # 获取JSON请求数据

    # 从 JSON 数据中提取特征，并确保转换为浮点数
    feature1 = float(data["feature1"][0]) if isinstance(data["feature1"], list) else float(data["feature1"])
    feature2 = float(data["feature2"][0]) if isinstance(data["feature2"], list) else float(data["feature2"])
    feature3 = float(data["feature3"][0]) if isinstance(data["feature3"], list) else float(data["feature3"])

    # 将特征组合成一个 NumPy 数组作为模型输入
    features = np.array([feature1, feature2, feature3]).reshape(1, -1)
    print(features)
    # 使用模型进行预测
    predictions = model.predict_classes(features)
    print(predictions)
    # 返回预测结果
    return jsonify(int(predictions[0]))
    # return jsonify(int(predictions[0]))

if __name__ == '__main__':
    app.run(debug=True)